//
//  SSNetworking.m
//  Journey
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SSNetworking.h"
#import "AFNetworking.h"
#import "Reachability.h"
@implementation SSNetworking

//GET请求
+(void)ssGETWithURL:(NSString *)url parameter:(NSDictionary *)dic success:(successBlock)sucBlock failure:(failureBlock)errorBlock network:(networkBlock)netBlock
{
    //逻辑:
    //1. 先从本地取数据,显示在界面上
    //2.再从网络获取数据
      //2.1如果有网,显示最新数据,更新本地数据
      //2.2如果没有网,提示用户"网络不给力"
    
     // 判断网络情况
       //****重要**** self在加号方法里代表类
    if ([self checkNetworkingStatues]) {
        //2.如果有网,则从网络请求
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //设置请求类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet  setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript", nil]];
        
       
        
        [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //回调
            sucBlock(responseObject);
            //更新本地数据
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            errorBlock(error);
            
            
        }];
        
    }else {
        //没有网络,从本地获取数据
        
        netBlock(NO);
    }
}







//检查网络状况
+(BOOL)checkNetworkingStatues
{
    //创建对象
    //reachabilityForInternetConnection 会自动获取网络状态,并将信息存入对象中
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    if (reachability.currentReachabilityStatus == NotReachable) {
        //无网络
        return NO;
    }else{
        return YES;
    }
    
    
}

@end
